"""
# -*- coding: utf-8 -*-
# @Time    : 2023/6/13 17:42
# @Author  : 王摇摆
# @FileName: Multiclass1.py.py
# @Software: PyCharm
# @Blog    ：https://blog.csdn.net/weixin_44943389?type=blog
多类别分类器：1对多
"""
from sklearn import metrics
from sklearn.datasets import load_digits
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.multiclass import OneVsRestClassifier
# import warnings # 解决方法3：直接屏蔽警告信息
# warnings.filterwarnings("ignore")
from sklearn.preprocessing import StandardScaler

digits = load_digits()
print('\n1. 数据集加载成功')

# 数据集切分
X_train, X_test, y_train, y_test = train_test_split(digits['data'], digits['target'], test_size=0.2)
# 对训练集和测试集的特征数据进行缩放
scaler = StandardScaler()  # 解决方法2：对数据集进行缩放，进行归一化标准化
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
print('\n2. 数据集预处理成功')

ova_lr = OneVsRestClassifier(LogisticRegression(solver='lbfgs', max_iter=1000))
print('\n3. 一对多分类模型已创建成功')

ova_lr.fit(X_train_scaled, y_train)
print('\n4. 模型已训练完毕')


print('\n===============正在输出训练结果======================')
print('OvA LR - Accuracy (Train): %.4g' % metrics.accuracy_score(y_train, ova_lr.predict(X_train)))
print("OvA LR - Accuracy (Test): %.4g" % metrics.accuracy_score(y_test, ova_lr.predict(X_test)))
